package com.flowershop.servlet;

import com.flowershop.model.Flower;
import com.flowershop.service.FlowerService;
import com.flowershop.service.OrderService;
import com.flowershop.model.Order;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/flower/*")
public class FlowerServlet extends HttpServlet {
    private FlowerService flowerService = new FlowerService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        if (pathInfo == null || pathInfo.equals("/")) {
            // 获取所有花卉列表
            List<Flower> flowers = flowerService.getAllFlowers();
            request.setAttribute("flowers", flowers);
            request.getRequestDispatcher("/WEB-INF/views/flower/list.jsp").forward(request, response);
        } else if (pathInfo.equals("/add")) {
            // 显示添加花卉表单
            request.getRequestDispatcher("/WEB-INF/views/flower/add.jsp").forward(request, response);
        } else if (pathInfo.startsWith("/edit/")) {
            // 显示编辑花卉表单
            int id = Integer.parseInt(pathInfo.substring(6));
            Flower flower = flowerService.getFlowerById(id);
            request.setAttribute("flower", flower);
            request.getRequestDispatcher("/WEB-INF/views/flower/edit.jsp").forward(request, response);
        } else if (pathInfo.equals("/orders")) {
            // 获取所有订单
            OrderService orderService = new OrderService();
            List<Order> orders = orderService.getAllOrders();
            request.setAttribute("orders", orders);
            request.getRequestDispatcher("/WEB-INF/views/flower/orders.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        if (pathInfo.equals("/add")) {
            // 处理添加花卉
            Flower flower = new Flower();
            flower.setName(request.getParameter("name"));
            flower.setPrice(new BigDecimal(request.getParameter("price")));
            flower.setStock(Integer.parseInt(request.getParameter("stock")));
            flower.setCategoryId(Integer.parseInt(request.getParameter("categoryId")));
            flower.setDescription(request.getParameter("description"));
            flower.setImageUrl(request.getParameter("imageUrl"));
            
            flowerService.addFlower(flower);
            response.sendRedirect(request.getContextPath() + "/flower");
        } else if (pathInfo.equals("/edit")) {
            // 处理编辑花卉
            Flower flower = new Flower();
            flower.setId(Integer.parseInt(request.getParameter("id")));
            flower.setName(request.getParameter("name"));
            flower.setPrice(new BigDecimal(request.getParameter("price")));
            flower.setStock(Integer.parseInt(request.getParameter("stock")));
            flower.setCategoryId(Integer.parseInt(request.getParameter("categoryId")));
            flower.setDescription(request.getParameter("description"));
            flower.setImageUrl(request.getParameter("imageUrl"));
            
            flowerService.updateFlower(flower);
            response.sendRedirect(request.getContextPath() + "/flower");
        } else if (pathInfo.equals("/order/status")) {
            response.setContentType("application/json;charset=UTF-8");
            
            Integer orderId = Integer.parseInt(request.getParameter("orderId"));
            String status = request.getParameter("status");
            
            OrderService orderService = new OrderService();
            boolean success = orderService.updateOrderStatus(orderId, status);
            
            response.getWriter().write("{\"success\":" + success + "}");
            return;
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if (pathInfo.startsWith("/delete/")) {
            int id = Integer.parseInt(pathInfo.substring(8));
            flowerService.deleteFlower(id);
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }
} 